home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Offline Browsing / HTTrack.exe / data1.cab / Sources / src / htsmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-28  |  64.1 KB  |  1,765 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       main routine (first called)                            */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #include "htsmain.h"
  39.  
  40. #include "htsglobal.h"
  41. #include "httrack.h"
  42. #include "htsdefines.h"
  43. #include "htsalias.h"
  44. #include <ctype.h>
  45. #if HTS_WIN
  46. #else
  47. #ifndef HTS_DO_NOT_USE_UID
  48. /* setuid */
  49. #include <pwd.h>
  50. #include <unistd.h>
  51. #endif
  52. #endif
  53.  
  54. extern int exit_xh;          // sortir prΘmaturΘment
  55.  
  56. // Add a command in the argc/argv
  57. #define cmdl_add(token,argc,argv,buff,ptr) \
  58.   argv[argc]=(buff+ptr); \
  59.   strcpy(argv[argc],token); \
  60.   ptr += (strlen(argv[argc])+1); \
  61.   argc++
  62.  
  63. // Insert a command in the argc/argv
  64. #define cmdl_ins(token,argc,argv,buff,ptr) \
  65.   { \
  66.   int i; \
  67.   for(i=argc;i>0;i--)\
  68.   argv[i]=argv[i-1];\
  69.   } \
  70.   argv[0]=(buff+ptr); \
  71.   strcpy(argv[0],token); \
  72.   ptr += (strlen(argv[0])+1); \
  73.   argc++
  74.  
  75.  
  76. // Main, rΘcupΦre les paramΦtres et appelle le robot
  77. #if HTS_ANALYSTE!=2
  78. int main(int argc, char **argv) {
  79. #else
  80. int hts_main(int argc, char **argv) {
  81. #endif
  82.   char* x_argv[999];      // Patch pour argv et argc: en cas de rΘcupΘration de ligne de commande
  83.   char* x_argvblk=NULL;   // (reprise ou update)
  84.   int   x_ptr=0;          // offset
  85.   /*
  86.   char* x_argv2[999];     // Patch pour config
  87.   char* x_argvblk2=NULL;
  88.   */
  89.   //
  90.   int argv_url=-1;         // ==0 : utiliser cache et doit.log
  91.   char url[65536];         // URLS sΘparΘes par un espace
  92.   // the parametres
  93.   httrackp httrack;
  94.   int httrack_logmode=3;   // ONE log file
  95.   int recuperer=0;       // rΘcupΘrer un plantage (n'arrive jamais, α supprimer)
  96. #if HTS_WIN
  97. #if HTS_ANALYSTE!=2
  98.   WORD   wVersionRequested; /* requested version WinSock API */ 
  99.   WSADATA wsadata;        /* Windows Sockets API data */
  100. #endif
  101. #else
  102. #ifndef HTS_DO_NOT_USE_UID
  103.   int switch_uid=-1,switch_gid=-1;      /* setuid/setgid */
  104. #endif
  105.   int switch_chroot=0;                  /* chroot ? */
  106. #endif
  107.   //
  108.   url[0]='\0';
  109.   //
  110.  
  111.   // options par dΘfaut
  112.   bzero((char *)&httrack, sizeof(httrackp));
  113.   httrack.wizard=2;   // wizard automatique
  114.   httrack.quiet=0;     // questions
  115.   //  
  116.   httrack.travel=0;   // mΩme adresse
  117.   httrack.depth=9999; // mirror total par dΘfaut
  118.   httrack.extdepth=0; // mais pas α l'extΘrieur
  119.   httrack.seeker=1;   // down 
  120.   httrack.urlmode=2;  // relatif par dΘfaut
  121.   httrack.debug=0;    // pas de dΘbug en plus
  122.   httrack.getmode=3;  // linear scan
  123.   httrack.maxsite=-1; // taille max site (aucune)
  124.   httrack.maxfile_nonhtml=-1; // taille max fichier non html
  125.   httrack.maxfile_html=-1;    // idem pour html
  126.   httrack.maxsoc=8;     // nbre socket max
  127.   httrack.fragment=-1;  // pas de fragmentation
  128.   httrack.nearlink=0;   // ne pas prendre les liens non-html "adjacents"
  129.   httrack.makeindex=1;  // faire un index
  130.   httrack.kindex=0;     // index 'keyword'
  131.   httrack.delete_old=1; // effacer anciens fichiers
  132.   httrack.makestat=0;  // pas de fichier de stats
  133.   httrack.maketrack=0; // ni de tracking
  134.   httrack.timeout=120; // timeout par dΘfaut (2 minutes)
  135.   httrack.cache=1;     // cache prioritaire
  136.   httrack.shell=0;     // pas de shell par defaut
  137.   httrack.proxy.active=0;    // pas de proxy
  138.   httrack.user_agent_send=1; // envoyer un user-agent
  139.   strcpy(httrack.user_agent,"Mozilla/4.5 (compatible; HTTrack 3.0x; Windows 98)");
  140.   httrack.savename_83=0;     // noms longs par dΘfaut
  141.   httrack.savename_type=0;   // avec structure originale
  142.   httrack.parsejava=1;       // parser classes
  143.   httrack.hostcontrol=0;     // PAS de control host pour timeout et traffic jammer
  144.   httrack.retry=2;           // 2 retry par dΘfaut
  145.   httrack.errpage=1;         // copier ou gΘnΘrer une page d'erreur en cas d'erreur (404 etc.)
  146.   httrack.check_type=1;      // vΘrifier type si inconnu (cgi,asp..) SAUF / considΘrΘ comme html
  147.   httrack.all_in_cache=0;    // ne pas tout stocker en cache
  148.   httrack.robots=2;          // traiter les robots.txt
  149.   httrack.external=0;        // liens externes normaux
  150.   httrack.mirror_first_page=0;  // pas mode mirror links
  151.   httrack.accept_cookie=1;   // gΘrer les cookies
  152.   httrack.cookie=NULL;
  153.   httrack.http10=0;          // laisser http/1.1
  154.   httrack.tolerant=0;        // ne pas accepter content-length incorrect
  155.   httrack.parseall=1;        // tout parser (tags inconnus, par exemple)
  156.   httrack.norecatch=0;       // ne pas reprendre les fichiers effacΘs par l'utilisateur
  157.   httrack.verbosedisplay=0;  // pas d'animation texte
  158.   strcpy(httrack.footer,HTS_DEFAULT_FOOTER);
  159.   httrack.ftp_proxy=1;       // proxy http pour ftp
  160.   strcpy(httrack.filelist,"");
  161.   strcpy(httrack.lang_iso,"en, *");
  162.   //
  163.   httrack.log=stdout;
  164.   httrack.errlog=stderr;
  165.   httrack.flush=1;           // flush sur les fichiers log
  166.   httrack.aff_progress=0;
  167.   httrack.keyboard=0;
  168.   //
  169.   strcpy(httrack.path_html,"");
  170.   strcpy(httrack.path_log,"");
  171.   //
  172.   httrack.maxlink=100000;    // 100,000 liens max par dΘfaut (400Kb)
  173.   httrack.maxfilter=500;     // 500 filtres max par dΘfaut (400Kb)
  174.   httrack.maxcache=1048576*32;   // a peu prΦs 32Mo en cache max -- OPTION NON PARAMETRABLE POUR L'INSTANT --
  175.   //httrack.maxcache_anticipate=256;  // maximum de liens α anticiper
  176.   httrack.maxtime=-1;        // temps max en secondes
  177.   httrack.maxrate=-1;        // pas de taux maxi
  178.   httrack.maxconn=10;        // nombre connexions/s
  179.   httrack.waittime=-1;      // wait until.. hh*3600+mm*60+ss
  180.   //
  181.   httrack.exec=argv[0];
  182.   //
  183.   _DEBUG_HEAD=0;            // pas de debuggage en tΩtes
  184.   
  185. #if HTS_WIN
  186. #if HTS_ANALYSTE!=2
  187.   {
  188.     int stat;
  189.     wVersionRequested = 0x0101;
  190.     stat = WSAStartup( wVersionRequested, &wsadata );
  191.     if (stat != 0) {
  192.       HTS_PANIC_PRINTF("Winsock not found!\n");
  193.       return -1;
  194.     } else if (LOBYTE(wsadata.wVersion) != 1  && HIBYTE(wsadata.wVersion) != 1) {
  195.       HTS_PANIC_PRINTF("WINSOCK.DLL does not support version 1.1\n");
  196.       WSACleanup();
  197.       return -1;
  198.     }
  199.   }
  200. #endif
  201. #endif
  202.  
  203.   /* First test: if -#R then only launch ftp */
  204.   if (argc > 2) {
  205.     if (strcmp(argv[1],"-#R")==0) {
  206.       if (argc==6) {
  207.         lien_back r;
  208.         char* path;
  209.         FILE* fp;
  210.         strcpy(r.url_adr,argv[2]);
  211.         strcpy(r.url_fil,argv[3]);
  212.         strcpy(r.url_sav,argv[4]);
  213.         path=argv[5];
  214.         r.status=1000;
  215.         run_launch_ftp(&r);
  216.         fp=fopen(fconv(path),"wb");
  217.         if (fp) {
  218.           fprintf(fp,"%d %s",r.r.statuscode,r.r.msg);
  219.           fclose(fp); fp=NULL;
  220.           rename(fconv(path),fconcat(path,".ok"));
  221.         } else remove(fconv(path));
  222.       } else {
  223.         printf("htsftp error, wrong parameter number (%d)\n",argc);
  224.       }
  225.       exit(0);   // pas _exit()
  226.     }
  227.   }
  228.  
  229.   // ok, non ftp, continuer
  230.  
  231.  
  232.   /* filter CR, LF, TAB.. */
  233.   {
  234.     int na;
  235.     for(na=1;na<argc;na++) {
  236.       char* a;
  237.       while( (a=strchr(argv[na],'\x0d')) ) *a=' ';
  238.       while( (a=strchr(argv[na],'\x0a')) ) *a=' ';
  239.       while( (a=strchr(argv[na],9)) )      *a=' ';
  240.       /* equivalent to "empty parameter" */
  241.       if ((strcmp(argv[na],HTS_NOPARAM)==0) || (strcmp(argv[na],HTS_NOPARAM2)==0))        // (none)
  242.         strcpy(argv[na],"\"\"");
  243.       if (strncmp(argv[na],"-&",2)==0)
  244.         argv[na][1]='%';
  245.     }
  246.   }
  247.  
  248.  
  249.  
  250.   /* create x_argvblk buffer for transformed command line */
  251.   {
  252.     int current_size=0;
  253.     int size;
  254.     int na;
  255.     for(na=0;na<argc;na++)
  256.       current_size += (strlen(argv[na]) + 1);
  257.     if ((size=fsize("config"))>0)
  258.       current_size += size;
  259.     x_argvblk=(char*) malloct(current_size+32768);
  260.     if (x_argvblk == NULL) {
  261.       HTS_PANIC_PRINTF("Error, not enough memory");
  262.       return -1;
  263.     }
  264.     x_argvblk[0]='\0';
  265.     x_ptr=0;
  266.   }
  267.  
  268.   /* Create new argc/argv, replace alias, count URLs, treat -h, -q, -i */
  269.   {
  270.     char _tmp_argv[2][HTS_CDLMAXSIZE];
  271.     char* tmp_argv[2];
  272.     char tmp_error[HTS_CDLMAXSIZE];
  273.     int tmp_argc;
  274.     int x_argc=0;
  275.     int na;
  276.     tmp_argv[0]=_tmp_argv[0];
  277.     tmp_argv[1]=_tmp_argv[1];
  278.     //
  279.     argv_url=0;       /* pour comptage */
  280.     //
  281.     cmdl_add(argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  282.     na=1;             /* commencer aprΦs nom_prg */
  283.     while(na<argc) {
  284.       int result=1;
  285.       tmp_argv[0][0]=tmp_argv[1][0]='\0';
  286.  
  287.       /* VΘrifier argv[] non vide */
  288.       if (strnotempty(argv[na])) {
  289.         
  290.         /* VΘrifier Commande (alias) */
  291.         result=optalias_check(argc,(const char * const *)argv,na,
  292.           &tmp_argc,(char**)tmp_argv,tmp_error);
  293.         if (!result) {
  294.           HTS_PANIC_PRINTF(tmp_error);
  295.           return -1;
  296.         }
  297.         
  298.         /* Copier */
  299.         cmdl_add(tmp_argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  300.         if (tmp_argc > 1) {
  301.           cmdl_add(tmp_argv[1],x_argc,x_argv,x_argvblk,x_ptr);
  302.         }
  303.         
  304.         /* Compter URLs et dΘtecter -i,-q.. */
  305.         if (tmp_argc == 1) {           /* pas -P & co */
  306.           if (!cmdl_opt(tmp_argv[0])) {   /* pas -c0 & co */
  307.             if (argv_url>=0)
  308.               argv_url++;
  309.           } else {
  310.             if (strcmp(tmp_argv[0],"-h")==0) {
  311.               help(argv[0],!httrack.quiet);
  312.               return 0;
  313.             } else {
  314.               if (strncmp(tmp_argv[0],"--",2)) {   /* pas */
  315.                 if ((strchr(tmp_argv[0],'q')!=NULL))
  316.                   httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  317.                 if ((strchr(tmp_argv[0],'i')!=NULL)) {  // doit.log!
  318.                   argv_url=-1;        /* forcer */
  319.                   httrack.quiet=1;
  320.                 }
  321.               }
  322.             }
  323.           }
  324.         } else if (tmp_argc == 2) {
  325.           if ((strcmp(tmp_argv[0],"-%L")==0)) {  // liste d'URLs
  326.             if (argv_url>=0)
  327.               argv_url++;        /* forcer */
  328.           }
  329.         }
  330.       }
  331.  
  332.       na+=result;
  333.     }
  334.     if (argv_url<0)
  335.       argv_url=0;
  336.  
  337.     /* Nouveaux argc et argv */
  338.     argv=x_argv;
  339.     argc=x_argc;
  340.   }
  341.  
  342.  
  343.  
  344.  
  345.   // Ici on ajoute les arguments de config
  346. /*
  347.   if (fexist("config")) {    // configuration        
  348.     x_argvblk2=(char*) calloct(32768,1);
  349.  
  350.     if (x_argvblk2!=NULL) {
  351.       FILE* fp;
  352.       int x_argc2;
  353.           
  354.       //strcpy(x_argvblk2,"httrack ");
  355.       fp=fopen("config","rb");
  356.       if (fp) {
  357.         linput(fp,x_argvblk2+strlen(x_argvblk2),32000);
  358.         fclose(fp); fp=NULL;
  359.     
  360.         // calculer arguments selon derniers arguments
  361.         x_argv2[0]=argv[0];
  362.         x_argc2=1;
  363.         {
  364.           char* p=x_argvblk2;
  365.           do {
  366.             x_argv2[x_argc2++]=p;
  367.             p=strchr(p,' ');
  368.             if (p) {
  369.               *p=0;    // octet nul (tableau)
  370.               p++;
  371.             }            
  372.           } while(p!=NULL);
  373.         }
  374.         // recopier arguments actuels (pointeurs uniquement)
  375.         {
  376.          int na;
  377.           for(na=1;na<argc;na++) {
  378.             x_argv2[x_argc2++]=argv[na];
  379.           }
  380.         }
  381.         argc=x_argc2;      // nouvel argc
  382.         argv=x_argv2;      // nouvel argv
  383.       }
  384.     }
  385.   }
  386. */
  387.  
  388.  
  389.   // Traiter l'option -O en premier!
  390.   { 
  391.     char* com;
  392.     int na;
  393.     
  394.     for(na=1;na<argc;na++) {
  395.       
  396.       if (argv[na][0]=='"') {
  397.         char tempo[HTS_CDLMAXSIZE];
  398.         strcpy(tempo,argv[na]+1);
  399.         if (tempo[strlen(tempo)-1]!='"') {
  400.           char s[HTS_CDLMAXSIZE];
  401.           sprintf(s,"Missing quote in %s",argv[na]);
  402.           HTS_PANIC_PRINTF(s);
  403.           return -1;
  404.         }
  405.         tempo[strlen(tempo)-1]='\0';
  406.         strcpy(argv[na],tempo);
  407.       }
  408.       
  409.       if (cmdl_opt(argv[na])) { // option
  410.         com=argv[na]+1;
  411.         
  412.         while(*com) {
  413.           switch(*com) {
  414.           case 'O':    // output path
  415.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  416.               HTS_PANIC_PRINTF("Option O needs to be followed by a blank space, and a path (or path,path)");
  417.               printf("Example: -O /binary/\n");
  418.               printf("Example: -O /binary/,/log/\n");
  419.               return -1;
  420.             } else {
  421.               char* a;
  422.               na++;
  423.               strcpy(httrack.path_html,"");
  424.               strcpy(httrack.path_log,"");
  425.               a=strstr(argv[na],"\",\"");  // rechercher en premier, au cas ou -O "c:\pipo,test","c:\test"
  426.               if (!a)
  427.                 a=strchr(argv[na],',');  // 2 path
  428.               else
  429.                 a++;  // position ,
  430.               if (a) {
  431.                 strncat(httrack.path_html,argv[na],(int) a-(int) argv[na]);
  432.                 strcat(httrack.path_log,a+1);
  433.               } else {
  434.                 strcpy(httrack.path_log,argv[na]);
  435.                 strcpy(httrack.path_html,argv[na]);
  436.               }
  437.               // Eliminer les cas comme -O "C:\mirror\"
  438.               if (httrack.path_log[0]=='"') {  // Guillemets
  439.                 char tmp[256];
  440.                 strcpy(tmp,httrack.path_log+1);
  441.                 if (tmp[strlen(tmp)-1]=='"')
  442.                   tmp[strlen(tmp)-1]='\0';
  443.                 strcpy(httrack.path_log,tmp);
  444.               }
  445.               if (httrack.path_html[0]=='"') {
  446.                 char tmp[256];
  447.                 strcpy(tmp,httrack.path_html+1);
  448.                 if (tmp[strlen(tmp)-1]=='"')
  449.                   tmp[strlen(tmp)-1]='\0';
  450.                 strcpy(httrack.path_html,tmp);
  451.               }
  452.               check_path(httrack.path_log);
  453.               check_path(httrack.path_html);
  454.               
  455.               //printf("-->%s\n%s\n",httrack.path_html,httrack.path_log);
  456.               
  457.             }
  458.             break;
  459.           }  // switch
  460.           com++;    
  461.         }  // while
  462.         
  463.       }  // arg
  464.       
  465.     }  // for
  466.   }  // traiter -O
  467.   
  468.   /* if doit.log exists, or if new URL(s) defined, 
  469.      then DO NOT load standard config files */
  470.   /* (config files are added in doit.log) */
  471. #if DEBUG_STEPS
  472.   printf("Loading httrackrc/doit.log\n");
  473. #endif
  474.   /* recreate a doit.log (no old doit.log or new URLs (and parameters)) */
  475.   if ( (!fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) || (argv_url>0) ) {
  476.     if (!optinclude_file(fconcat(httrack.path_log,HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr))
  477.       if (!optinclude_file(HTS_HTTRACKRC,&argc,argv,x_argvblk,&x_ptr)) {
  478.         if (!optinclude_file(fconcat(hts_gethome(),"/"HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr)) {
  479. #ifdef HTS_HTTRACKCNF
  480.           optinclude_file(HTS_HTTRACKCNF,&argc,argv,x_argvblk,&x_ptr);
  481. #endif
  482.         }
  483.       }
  484.   } 
  485.   /* load doit.log and insert in current command line */
  486.   else {
  487.     FILE* fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"rb");
  488.     if (fp) {
  489.       int insert_after=1;     /* insΘrer aprΦs nom au dΘbut */
  490.       //
  491.       char buff[8192];
  492.       char *p,*lastp;
  493.       linput(fp,buff,8000);
  494.       fclose(fp); fp=NULL;
  495.       p=buff;
  496.       do {
  497.         int insert_after_argc;
  498.         // read next
  499.         lastp=p;
  500.         if (p) {
  501.           p=next_token(p,1);
  502.           if (p) {
  503.             *p=0;    // null
  504.             p++;
  505.           }
  506.         }
  507.  
  508.         /* Insert parameters BUT so that they can be in the same order */
  509.         if (lastp) {
  510.           if (strnotempty(lastp)) {
  511.             insert_after_argc=argc-insert_after;
  512.             cmdl_ins(lastp,insert_after_argc,(argv+insert_after),x_argvblk,x_ptr);
  513.             argc=insert_after_argc+insert_after;
  514.             insert_after++;
  515.           }
  516.         }
  517.       } while(lastp!=NULL);
  518.       //fclose(fp);
  519.     }
  520.   }
  521.  
  522.  
  523.   // Existence d'un cache - pas de new mais un old.. renommer
  524. #if DEBUG_STEPS
  525.   printf("Checking cache\n");
  526. #endif
  527.   if ( (!fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) || (!fexist(fconcat(httrack.path_log,"hts-cache/new.ndx"))) ) {
  528.     if ( (fexist(fconcat(httrack.path_log,"hts-cache/old.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) ) {
  529.       remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  530.       remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  531.       //remove(fconcat(httrack.path_log,"hts-cache/new.lst"));
  532.       rename(fconcat(httrack.path_log,"hts-cache/old.dat"),fconcat(httrack.path_log,"hts-cache/new.dat"));
  533.       rename(fconcat(httrack.path_log,"hts-cache/old.ndx"),fconcat(httrack.path_log,"hts-cache/new.ndx"));
  534.       //rename(fconcat(httrack.path_log,"hts-cache/old.lst"),fconcat(httrack.path_log,"hts-cache/new.lst"));
  535.     }
  536.   }
  537.  
  538.   /* Interrupted mirror detected */
  539.   if (!httrack.quiet) {
  540.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {
  541.       /* Old cache */
  542.       if ( (fexist(fconcat(httrack.path_log,"hts-cache/old.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) ) {
  543.         fprintf(httrack.log,"Warning!\n");
  544.         fprintf(httrack.log,"An aborted mirror has been detected!\nThe current temporary cache is required for any update operation and only contains data downloaded during the last aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files\n");
  545.         fprintf(httrack.log,"Please restart HTTrack with --quiet (-q) option to override this message!\n");
  546.         exit(0);
  547.       }
  548.     }
  549.   }
  550.     
  551.   // remplacer "macros" comme --spider
  552.   // permet de lancer httrack sans a avoir α se rappeler de syntaxes comme p0C0I0Qc32 ..
  553. #if DEBUG_STEPS
  554.   printf("Checking last macros\n");
  555. #endif
  556.   {
  557.     int i;
  558.     for(i=0;i<argc;i++) {
  559. #if DEBUG_STEPS
  560.       printf("Checking #%d:\n",argv[i]);
  561.       printf("%s\n",argv[i]);
  562. #endif
  563.       if (argv[i][0]=='-') {
  564.         if (argv[i][1]=='-') {  // --xxx
  565.           // Note: on fait attention α ne pas Θcrire plus qu'il n'y avait avant..
  566.           /*
  567.           if (strfield2(argv[i]+2,"spider"))       // mode spider (scan+log)
  568.             strcpy(argv[i]+1,"p0C0I0t");
  569.           else if (strfield2(argv[i]+2,"wide-spider"))   // spider
  570.             strcpy(argv[i]+1,"p0C0I0c32t");
  571.           else if (strfield2(argv[i]+2,"tiny-spider"))   // i am a little grasshoper
  572.             strcpy(argv[i]+1,"p0C0I0c1t");
  573.           else if (strfield2(argv[i]+2,"testsite"))       // mode test site (scan+log)
  574.             strcpy(argv[i]+1,"p0C0I0t");
  575.           else if (strfield2(argv[i]+2,"wide-testsite"))
  576.             strcpy(argv[i]+1,"p0C0I0c32t");
  577.           else if (strfield2(argv[i]+2,"tiny-testsite"))
  578.             strcpy(argv[i]+1,"p0C0I0c1t");
  579.           //
  580.           else if (strfield2(argv[i]+2,"testlinks"))       // mode test bookmark (scan+log)
  581.             strcpy(argv[i]+1,"r1p0C0I0t");
  582.           else if (strfield2(argv[i]+2,"wide-testlinks"))
  583.             strcpy(argv[i]+1,"r1p0C0I0c32t");
  584.           else if (strfield2(argv[i]+2,"tiny-testlinks"))
  585.             strcpy(argv[i]+1,"r1p0C0I0c1t");
  586.           else if ( (strfield2(argv[i]+2,"testlink")) || (strfield2(argv[i]+2,"bookmark"))) {
  587.             HTS_PANIC_PRINTF("Please use --testlinks to check links in a page");
  588.             return -1;                
  589.           }
  590.           //
  591.           else if (strfield2(argv[i]+2,"mirrorlinks"))       // mode test bookmark (scan+log)
  592.             strcpy(argv[i]+1,"Y");
  593.           else if (strfield2(argv[i]+2,"wide-mirrorlinks"))       // mode test bookmark (scan+log)
  594.             strcpy(argv[i]+1,"Yc32");
  595.           else if (strfield2(argv[i]+2,"tiny-mirrorlinks"))       // mode test bookmark (scan+log)
  596.             strcpy(argv[i]+1,"Yc1");
  597.           //
  598.           else if (strfield2(argv[i]+2,"mirror"))  // miroir (mode par dΘfaut)
  599.             strcpy(argv[i]+1,"");
  600.           else if (strfield2(argv[i]+2,"wide-mirror"))
  601.             strcpy(argv[i]+1,"c32");
  602.           else if (strfield2(argv[i]+2,"tiny-mirror"))
  603.             strcpy(argv[i]+1,"c1");
  604.           //
  605.           else if (strfield2(argv[i]+2,"testscan"))  // scan sans log
  606.             strcpy(argv[i]+1,"p0C0I0Q");
  607.           else if (strfield2(argv[i]+2,"wide-testscan"))
  608.             strcpy(argv[i]+1,"p0C0I0c32Q");
  609.           else if (strfield2(argv[i]+2,"tiny-testscan"))
  610.             strcpy(argv[i]+1,"p0C0I0c1Q");
  611.           else if ((strfield2(argv[i]+2,"scan")) || (strfield2(argv[i]+2,"test")) || (strfield2(argv[i]+2,"check"))) {
  612.             HTS_PANIC_PRINTF("Please use --spider to check all links in a site");
  613.             return -1;    
  614.           }
  615.           //
  616.           else if (strfield2(argv[i]+2,"skeleton"))    // rΘcupΘrer squelette (html)
  617.             strcpy(argv[i]+1,"p1");
  618.           else if (strfield2(argv[i]+2,"wide-skeleton"))
  619.             strcpy(argv[i]+1,"p1c32");
  620.           else if (strfield2(argv[i]+2,"tiny-skeleton"))
  621.             strcpy(argv[i]+1,"p1c1");
  622.           //
  623.           else if (strfield2(argv[i]+2,"get"))     // rΘcupΘrer fichiers isolΘs
  624.             strcpy(argv[i]+1,"qg");
  625.           else if (strfield2(argv[i]+2,"update"))        // update: pas de question
  626.             strcpy(argv[i]+1,"iC2");
  627.           else if (strfield2(argv[i]+2,"continue"))      // continue, cache prio
  628.             strcpy(argv[i]+1,"iC1");
  629.           else if (strfield2(argv[i]+2,"restart"))       // idem
  630.             strcpy(argv[i]+1,"iC1");
  631.           //
  632.           else if (strfield2(argv[i]+2,"sucker"))        // web sucker...toi aussi suce le web!
  633.             strcpy(argv[i]+1,"r99");
  634.     //
  635.           else if (strfield2(argv[i]+2,"help"))          // un peu d'aide
  636.             strcpy(argv[i]+1,"h");
  637.           else if (strfield2(argv[i]+2,"documentation"))
  638.             strcpy(argv[i]+1,"h");
  639.           else if (strfield2(argv[i]+2,"doc"))
  640.             strcpy(argv[i]+1,"h");
  641.           //
  642.           else if (strfield2(argv[i]+2,"wide"))
  643.             strcpy(argv[i]+1,"c32");
  644.           else if (strfield2(argv[i]+2,"tiny"))
  645.             strcpy(argv[i]+1,"c1");
  646.           else if (strfield2(argv[i]+2,"ultrawide"))  // oulαα..
  647.             strcpy(argv[i]+1,"c48");
  648.           //
  649.           else if (strfield2(argv[i]+2,"http10"))     // use if possible HTTP/1.0
  650.             strcpy(argv[i]+1,"%h");
  651.           //
  652.           else if ( (strfield2(argv[i]+2,"filelist"))     // file list
  653.                   || (strfield2(argv[i]+2,"list")) )
  654.             strcpy(argv[i]+1,"%L");
  655.  
  656.           else */
  657.           if ((strfield2(argv[i]+2,"clean")) || (strfield2(argv[i]+2,"tide"))) {  // nettoyer
  658.             strcpy(argv[i]+1,"");
  659.             if (fexist(fconcat(httrack.path_log,"hts-log.txt")))
  660.               remove(fconcat(httrack.path_log,"hts-log.txt"));
  661.             if (fexist(fconcat(httrack.path_log,"hts-err.txt")))
  662.               remove(fconcat(httrack.path_log,"hts-err.txt"));
  663.             if (fexist(fconcat(httrack.path_html,"index.html")))
  664.               remove(fconcat(httrack.path_html,"index.html"));
  665.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.dat")))
  666.               remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  667.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))
  668.               remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  669.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.dat")))
  670.               remove(fconcat(httrack.path_log,"hts-cache/old.dat"));
  671.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx")))
  672.               remove(fconcat(httrack.path_log,"hts-cache/old.ndx"));
  673.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.lst")))
  674.               remove(fconcat(httrack.path_log,"hts-cache/new.lst"));
  675.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.lst")))
  676.               remove(fconcat(httrack.path_log,"hts-cache/old.lst"));
  677.             if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log")))
  678.               remove(fconcat(httrack.path_log,"hts-cache/doit.log"));
  679.             if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock")))
  680.               remove(fconcat(httrack.path_log,"hts-in_progress.lock"));
  681.             rmdir(fconcat(httrack.path_log,"hts-cache"));
  682.             //
  683.           } else if (strfield2(argv[i]+2,"catchurl")) {      // capture d'URL via proxy temporaire!
  684.             argv_url=1;     // forcer a passer les parametres
  685.             strcpy(argv[i]+1,"#P");
  686.             //
  687.           } else if (strfield2(argv[i]+2,"updatehttrack")) {
  688. #ifdef _WIN32
  689.             char s[HTS_CDLMAXSIZE];
  690.             sprintf(s,"%s not available in this version",argv[i]);
  691.             HTS_PANIC_PRINTF(s);
  692.             return -1;
  693. #else
  694. #if 0
  695.             char _args[8][256];
  696.             char *args[8];
  697.             
  698.             printf("Cheking for updates...\n");
  699.             strcpy(_args[0],argv[0]);
  700.             strcpy(_args[1],"--get");
  701.             sprintf(_args[2],HTS_UPDATE_WEBSITE,HTS_PLATFORM,"");
  702.             strcpy(_args[3],"--quickinfo");
  703.             args[0]=_args[0];
  704.             args[1]=_args[1];
  705.             args[2]=_args[2];
  706.             args[3]=_args[3];
  707.             args[4]=NULL;
  708.             if (execvp(args[0],args)==-1) {
  709.             }
  710. #endif
  711. #endif
  712.           }
  713.           //
  714.           else {
  715.             char s[HTS_CDLMAXSIZE];
  716.             sprintf(s,"%s not recognized",argv[i]);
  717.             HTS_PANIC_PRINTF(s);
  718.             return -1;
  719.           }
  720.  
  721.         } 
  722.       }
  723.     }
  724.   }
  725.  
  726.   // Compter urls/jokers
  727.   /*
  728.   if (argv_url<=0) { 
  729.     int na;
  730.     argv_url=0;
  731.     for(na=1;na<argc;na++) {
  732.       if ( (strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0) ) {
  733.         na++;    // sauter nom de proxy
  734.       } else if (!cmdl_opt(argv[na])) { 
  735.         argv_url++;   // un de plus       
  736.       } else if (strcmp(argv[na],"-h")==0) {
  737.         help(argv[0],!httrack.quiet);
  738.         return 0;
  739.       } else {
  740.         if ((strchr(argv[na],'q')!=NULL))
  741.           httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  742.         if ((strchr(argv[na],'i')!=NULL)) {  // doit.log!
  743.           argv_url=0;
  744.           na=argc;
  745.         }
  746.       }
  747.     }
  748.   }  
  749.   */
  750.  
  751.   // Ici on ajoute les arguments qui ont ΘtΘ appelΘs avant au cas o∙ on rΘcupΦre une session
  752.   // Exemple: httrack www.truc.fr -L0 puis ^C puis httrack sans URL : ajouter URL prΘcΘdente
  753.   /*
  754.   if (argv_url==0) {
  755.     //if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  756.     if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) {    // un cache est prΘsent
  757.       
  758.       x_argvblk=(char*) calloct(32768,1);
  759.       
  760.       if (x_argvblk!=NULL) {
  761.         FILE* fp;
  762.         int x_argc;
  763.         
  764.         //strcpy(x_argvblk,"httrack ");
  765.         fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"rb");
  766.         if (fp) {
  767.           linput(fp,x_argvblk+strlen(x_argvblk),8192);
  768.           fclose(fp); fp=NULL;
  769.         }
  770.         
  771.         // calculer arguments selon derniers arguments
  772.         x_argv[0]=argv[0];
  773.         x_argc=1;
  774.         {
  775.           char* p=x_argvblk;
  776.           do {
  777.             x_argv[x_argc++]=p;
  778.             //p=strstr(p," ");
  779.             // exemple de chaine: "echo \"test\"" c:\a "\$0"
  780.             p=next_token(p,1);    // prochain token
  781.             if (p) {
  782.               *p=0;    // octet nul (tableau)
  783.               p++;
  784.             }            
  785.           } while(p!=NULL);
  786.         }
  787.         // recopier arguments actuels (pointeurs uniquement)
  788.         {
  789.           int na;
  790.           for(na=1;na<argc;na++) {
  791.             if (strcmp(argv[na],"-O") != 0)    // SAUF le path!
  792.               x_argv[x_argc++]=argv[na];
  793.             else
  794.               na++;
  795.           }
  796.         }
  797.         argc=x_argc;      // nouvel argc
  798.         argv=x_argv;      // nouvel argv
  799.       }
  800.       
  801.       
  802.     }
  803.     //}
  804.   }
  805.   */
  806.   
  807.   // VΘrifier quiet
  808.   /*
  809.   { 
  810.     int na;    
  811.     for(na=1;na<argc;na++) {
  812.       if (!cmdl_opt(argv[na])) { 
  813.         if ((strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0))
  814.           na++;    // sauter nom de proxy
  815.       } else {
  816.         if ((strchr(argv[na],'q')!=NULL) || (strchr(argv[na],'i')!=NULL))
  817.           httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  818.       }
  819.     }
  820.   }
  821.   */
  822.  
  823.   // Pas d'URL
  824. #if DEBUG_STEPS
  825.   printf("Checking URLs\n");
  826. #endif
  827.   if (argv_url==0) {
  828.     // PrΘsence d'un cache, que faire?..
  829.     if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  830.       if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) {    // un cache est prΘsent
  831.         if (x_argvblk!=NULL) {
  832.           int m;        
  833.           // Θtablir mode - mode cache: 1 (cache valide) 2 (cache α vΘrifier)
  834.           if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {    // cache prioritaire
  835.             m=1;
  836.             recuperer=1;
  837.           } else {
  838.             m=2;
  839.           }
  840.           httrack.cache=m;
  841.           
  842.           if (httrack.quiet==0) {  // sinon on continue automatiquement
  843.             HT_REQUEST_START;
  844.             HT_PRINT("A cache has been found"LF);
  845.             if (m==1) {
  846.               HT_PRINT("That means that a transfer has been aborted"LF);
  847.               HT_PRINT("OK to Continue ");
  848.             } else {
  849.               HT_PRINT("That means you can update faster the remote site(s)"LF);
  850.               HT_PRINT("OK to Update ");
  851.             }
  852.             HT_PRINT("httrack "); HT_PRINT(x_argvblk); HT_PRINT("?"LF);
  853.             HT_REQUEST_END;
  854.             if (!ask_continue()) return 0;
  855.           }
  856.           
  857.         } else {
  858.           HTS_PANIC_PRINTF("Error, not enough memory");
  859.           return -1;
  860.         }
  861.       } else { // log existe pas
  862.         HTS_PANIC_PRINTF("A cache has been found, but no command line");
  863.         printf("Please launch httrack with proper parameters to reuse the cache\n");
  864.         return -1;
  865.       }
  866.       
  867.     } else {    // aucune URL dΘfinie et pas de cache
  868. #if HTS_ANALYSTE!=2
  869.       if (httrack.quiet) {
  870. #endif
  871.         help(argv[0],!httrack.quiet);
  872.         return -1;
  873. #if HTS_ANALYSTE!=2
  874.       } else {
  875.         help_wizard(&httrack);
  876.         return -1;
  877.       }
  878. #endif
  879.       return 0;
  880.     }
  881.   } else {   // plus de 2 paramΦtres
  882.     // un fichier log existe?
  883.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {  // fichier lock?
  884.       //char s[32];
  885.       
  886.       httrack.cache=1;    // cache prioritaire
  887.       if (httrack.quiet==0) {
  888.         if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  889.           HT_REQUEST_START;
  890.           HT_PRINT("There is a lock-file in the current directory"LF);
  891.           HT_PRINT("That means that a mirror has not been terminated"LF);
  892.           HT_PRINT("Be sure you call httrack with proper parameters"LF);
  893.           HT_PRINT("(The cache allows you to restart faster the transfer)"LF);
  894.           HT_REQUEST_END;
  895.           if (!ask_continue()) return 0;
  896.         /*} else {
  897.           HT_PRINT("There is a lock-file in the current directory"LF);
  898.           HT_PRINT("That means that a mirror has not been terminated"LF);
  899.           HT_PRINT("There is no cache, HTtrack will not be able to use formerly loaded files"LF);*/
  900.         }
  901.       }
  902.     } else if (fexist(fconcat(httrack.path_html,"index.html"))) {
  903.       //char s[32];
  904.       httrack.cache=2;  // cache vient aprΦs test de validitΘ
  905.       if (httrack.quiet==0) {
  906.         if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  907.           HT_REQUEST_START;
  908.           HT_PRINT("There is an index.html in the current directory, and a cache"LF);
  909.           HT_PRINT("A site may have been mirrored here, that could mean that you want to update it"LF);
  910.           HT_PRINT("Be sure parameters are ok"LF);
  911.           HT_REQUEST_END;
  912.           if (!ask_continue()) return 0;
  913.         } else {
  914.           HT_REQUEST_START;
  915.           HT_PRINT("There is an index.html in the current directory, but no cache"LF);
  916.           HT_PRINT("A site may have been mirrored here, and erased.."LF);
  917.           HT_PRINT("Be sure parameters are ok"LF);
  918.           HT_REQUEST_END;
  919.           if (!ask_continue()) return 0;
  920.         }
  921.       }
  922.     }
  923.   }
  924.   
  925.   
  926.   // Treat parameters
  927.   // Traiter les paramΦtres
  928. #if DEBUG_STEPS
  929.   printf("Analyze parameters\n");
  930. #endif
  931.   { 
  932.     char* com;
  933.     int na;
  934.     
  935.     for(na=1;na<argc;na++) {
  936.  
  937.       if (argv[na][0]=='"') {
  938.         char tempo[HTS_CDLMAXSIZE];
  939.         strcpy(tempo,argv[na]+1);
  940.         if (tempo[strlen(tempo)-1]!='"') {
  941.           char s[HTS_CDLMAXSIZE];
  942.           sprintf(s,"Missing quote in %s",argv[na]);
  943.           HTS_PANIC_PRINTF(s);
  944.           return -1;
  945.         }
  946.         tempo[strlen(tempo)-1]='\0';
  947.         strcpy(argv[na],tempo);
  948.       }
  949.  
  950.       if (cmdl_opt(argv[na])) { // option
  951.         com=argv[na]+1;
  952.         
  953.         while(*com) {
  954.           switch(*com) {
  955.           case ' ': case 9: case '-': case '\0': break;
  956.             //
  957.           case 'h': help(argv[0],0); return 0;   // dΘja fait normalement
  958.             //
  959.           case 'g':    // rΘcupΘrer un (ou plusieurs) fichiers isolΘs
  960.             httrack.wizard=2;             // le wizard on peut plus s'en passer..
  961.             //httrack.wizard=0;             // pas de wizard
  962.             httrack.cache=0;              // ni de cache
  963.             httrack.makeindex=0;          // ni d'index
  964.             httrack_logmode=1;            // erreurs α l'Θcran
  965.             httrack.savename_type=1003;   // mettre dans le rΘpertoire courant
  966.             httrack.depth=0;              // ne pas explorer la page
  967.             break;
  968.           case 'w': httrack.wizard=2;    // wizard 'soft' (ne pose pas de questions)
  969.             httrack.travel=0;
  970.             httrack.seeker=1;
  971.             break;
  972.           case 'W': httrack.wizard=1;    // Wizard-Help (pose des questions)
  973.             httrack.travel=0;
  974.             httrack.seeker=1;
  975.             break;
  976.           case 'r':                      // n'est plus le recurse get bestial mais wizard itou!
  977.             if (isdigit((unsigned char)*(com+1))) {
  978.               sscanf(com+1,"%d",&httrack.depth);
  979.               while(isdigit((unsigned char)*(com+1))) com++;
  980.             } else httrack.depth=3;
  981.             break;
  982. /*
  983.           case 'r': httrack.wizard=0;
  984.             if (isdigit((unsigned char)*(com+1))) {
  985.               sscanf(com+1,"%d",&httrack.depth);
  986.               while(isdigit((unsigned char)*(com+1))) com++;
  987.             } else httrack.depth=3;
  988.             break;
  989. */
  990.             //
  991.             // note: les tests httrack.depth sont pour Θviter de faire
  992.             // un miroir du web (:-O) accidentelement ;-)
  993.           case 'a': /*if (httrack.depth==9999) httrack.depth=3;*/
  994.             httrack.travel=0+(httrack.travel&256); break;
  995.           case 'd': /*if (httrack.depth==9999) httrack.depth=3;*/
  996.             httrack.travel=1+(httrack.travel&256); break;
  997.           case 'l': /*if (httrack.depth==9999) httrack.depth=3;*/
  998.             httrack.travel=2+(httrack.travel&256); break;
  999.           case 'e': /*if (httrack.depth==9999) httrack.depth=3;*/
  1000.             httrack.travel=7+(httrack.travel&256); break;
  1001.           case 't': httrack.travel|=256; break;
  1002.           case 'n': httrack.nearlink=1; break;
  1003.           case 'x': httrack.external=1; break;
  1004.             //
  1005.           case 'U': httrack.seeker=2; break;
  1006.           case 'D': httrack.seeker=1; break;
  1007.           case 'S': httrack.seeker=0; break;
  1008.           case 'B': httrack.seeker=3; break;
  1009.             //
  1010.           case 'Y': httrack.mirror_first_page=1; break;
  1011.             //
  1012.           case 'q': case 'i': httrack.quiet=1; break;
  1013.             //
  1014.           case 'Q': httrack_logmode=0; break;
  1015.           case 'v': httrack_logmode=1; break;
  1016.           case 'f': httrack_logmode=2; if (*(com+1)=='2') httrack_logmode=3; while(isdigit((unsigned char)*(com+1))) com++; break;
  1017.             //
  1018.           //case 'A': httrack.urlmode=1; break;
  1019.           //case 'R': httrack.urlmode=2; break;
  1020.           case 'K': httrack.urlmode=0; 
  1021.             if (isdigit(*(com+1))) {
  1022.               sscanf(com+1,"%d",&httrack.urlmode);
  1023.               if (httrack.urlmode == 0)   // in fact K0 ==> K2
  1024.                                           // and K ==> K0
  1025.                 httrack.urlmode=2;
  1026.               while(isdigit((unsigned char)*(com+1))) com++; 
  1027.             }
  1028.             //if (*(com+1)=='0') { httrack.urlmode=2; com++; } break;
  1029.             //
  1030.           case 'c':
  1031.             if (isdigit((unsigned char)*(com+1))) {
  1032.               sscanf(com+1,"%d",&httrack.maxsoc);
  1033.               while(isdigit((unsigned char)*(com+1))) com++;
  1034.               httrack.maxsoc=max(httrack.maxsoc,1);     // FORCER A 1
  1035.             } else httrack.maxsoc=8;
  1036.             
  1037.             break;
  1038.             //
  1039.           case 'p': sscanf(com+1,"%d",&httrack.getmode); while(isdigit((unsigned char)*(com+1))) com++; break;
  1040.             //        
  1041.           case 'G': sscanf(com+1,"%d",&httrack.fragment); while(isdigit((unsigned char)*(com+1))) com++; break;
  1042.           case 'M': sscanf(com+1,"%d",&httrack.maxsite); while(isdigit((unsigned char)*(com+1))) com++; break;
  1043.           case 'm': sscanf(com+1,"%d",&httrack.maxfile_nonhtml); while(isdigit((unsigned char)*(com+1))) com++; 
  1044.             if (*(com+1)==',') {
  1045.               com++;
  1046.               sscanf(com+1,"%d",&httrack.maxfile_html); while(isdigit((unsigned char)*(com+1))) com++;
  1047.             } else httrack.maxfile_html=-1;
  1048.             break;
  1049.             //
  1050.           case 'T': sscanf(com+1,"%d",&httrack.timeout); while(isdigit((unsigned char)*(com+1))) com++; break;
  1051.           case 'J': sscanf(com+1,"%d",&httrack.rateout); while(isdigit((unsigned char)*(com+1))) com++; break;
  1052.           case 'R': sscanf(com+1,"%d",&httrack.retry); while(isdigit((unsigned char)*(com+1))) com++; break;
  1053.           case 'E': sscanf(com+1,"%d",&httrack.maxtime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1054.           case 'H': sscanf(com+1,"%d",&httrack.hostcontrol); while(isdigit((unsigned char)*(com+1))) com++; break;
  1055.           case 'A': sscanf(com+1,"%d",&httrack.maxrate); while(isdigit((unsigned char)*(com+1))) com++; break;
  1056.  
  1057.           case 'j': httrack.parsejava=1; if (*(com+1)=='0') { httrack.parsejava=0; com++; } break;
  1058.             //
  1059.           case 'I': httrack.makeindex=1; if (*(com+1)=='0') { httrack.makeindex=0; com++; } break;
  1060.             //
  1061.           case 'X': httrack.delete_old=1; if (*(com+1)=='0') { httrack.delete_old=0; com++; } break;
  1062.             //
  1063.           case 'b': sscanf(com+1,"%d",&httrack.accept_cookie); while(isdigit((unsigned char)*(com+1))) com++; break;
  1064.             //
  1065.           case 'N':
  1066.             if (strcmp(argv[na],"-N")==0) {    // Tout seul
  1067.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {  // erreur
  1068.                 HTS_PANIC_PRINTF("Option N needs a number, or needs to be followed by a blank space, and a string");
  1069.                 printf("Example: -N4\n");
  1070.                 return -1;
  1071.               } else {
  1072.                 na++;
  1073.                 if (strlen(argv[na])>=127) {
  1074.                   HTS_PANIC_PRINTF("Userdef structure string too long");
  1075.                   return -1;
  1076.                 }
  1077.                 strcpy(httrack.savename_userdef,argv[na]);
  1078.                 if (strnotempty(httrack.savename_userdef))
  1079.                   httrack.savename_type = -1;    // userdef!
  1080.                 else
  1081.                   httrack.savename_type = 0;    // -N "" : par dΘfaut
  1082.               }
  1083.             } else {
  1084.               sscanf(com+1,"%d",&httrack.savename_type); while(isdigit((unsigned char)*(com+1))) com++;
  1085.             }
  1086.             break;
  1087.           case 'L': sscanf(com+1,"%d",&httrack.savename_83); httrack.savename_83=!httrack.savename_83; while(isdigit((unsigned char)*(com+1))) com++; break;
  1088.           case 's': 
  1089.             if (isdigit((unsigned char)*(com+1))) {
  1090.               sscanf(com+1,"%d",&httrack.robots);
  1091.               while(isdigit((unsigned char)*(com+1))) com++;
  1092.             } else httrack.robots=1;
  1093. #if DEBUG_ROBOTS
  1094.             printf("robots.txt mode set to %d\n",httrack.robots);
  1095. #endif
  1096.             break;
  1097.           case 'o': sscanf(com+1,"%d",&httrack.errpage); while(isdigit((unsigned char)*(com+1))) com++; break;
  1098.           case 'u': sscanf(com+1,"%d",&httrack.check_type); while(isdigit((unsigned char)*(com+1))) com++; break;
  1099.             //
  1100.           case 'C': 
  1101.             if (isdigit((unsigned char)*(com+1))) {
  1102.               sscanf(com+1,"%d",&httrack.cache);
  1103.               while(isdigit((unsigned char)*(com+1))) com++;
  1104.             } else httrack.cache=1;
  1105.             break;
  1106.           case 'k': httrack.all_in_cache=1; break;
  1107.             //
  1108.           case 'z': httrack.debug=1; break;  // petit debug
  1109.           case 'Z': httrack.debug=2; break;  // GROS debug
  1110.             //
  1111.           case '&': case '%': {    // deuxiΦme jeu d'options
  1112.             com++;
  1113.             switch(*com) {
  1114.             case 'I': httrack.kindex=1; if (*(com+1)=='0') { httrack.kindex=0; com++; } break;   // Keyword Index
  1115.             case 'c': sscanf(com+1,"%d",&httrack.maxconn); while(isdigit((unsigned char)*(com+1))) com++; break;
  1116.             case 'e': sscanf(com+1,"%d",&httrack.extdepth); while(isdigit((unsigned char)*(com+1))) com++; break;
  1117.             case 'B': httrack.tolerant=1; if (*(com+1)=='0') { httrack.tolerant=0; com++; } break;   // HTTP/1.0 notamment
  1118.             case 'h': httrack.http10=1; if (*(com+1)=='0') { httrack.http10=0; com++; } break;   // HTTP/1.0
  1119.             case 'f': httrack.ftp_proxy=1; if (*(com+1)=='0') { httrack.ftp_proxy=0; com++; } break;   // proxy http pour ftp
  1120.             case 'P': httrack.parseall=1; if (*(com+1)=='0') { httrack.parseall=0; com++; } break;   // tout parser
  1121.             case 'n': httrack.norecatch=1; if (*(com+1)=='0') { httrack.norecatch=0; com++; } break;   // ne pas reprendre fichiers effacΘs localement
  1122.             case 's': httrack.sizehack=1; if (*(com+1)=='0') { httrack.sizehack=0; com++; } break;   // hack sur content-length
  1123.             case 'v': httrack.verbosedisplay=1; if (*(com+1)=='0') { httrack.verbosedisplay=0; com++; } break;
  1124.             case 'L':    // URL list
  1125.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1126.                 HTS_PANIC_PRINTF("Option %L needs to be followed by a blank space, and a text filename");
  1127.                 printf("Example: -%%L \"mylist.txt\"\n");
  1128.                 return -1;
  1129.               } else{
  1130.                 na++;
  1131.                 if (strlen(argv[na])>=254) {
  1132.                   HTS_PANIC_PRINTF("File list string too long");
  1133.                   return -1;
  1134.                 }
  1135.                 strcpy(httrack.filelist,argv[na]);
  1136.               }
  1137.               break;
  1138.               //
  1139.             case 'l': 
  1140.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1141.                 HTS_PANIC_PRINTF("Option %l needs to be followed by a blank space, and an ISO language code");
  1142.                 printf("Example: -%%l \"en\"\n");
  1143.                 return -1;
  1144.               } else{
  1145.                 na++;
  1146.                 if (strlen(argv[na])>=62) {
  1147.                   HTS_PANIC_PRINTF("Lang list string too long");
  1148.                   return -1;
  1149.                 }
  1150.                 strcpy(httrack.lang_iso,argv[na]);
  1151.               }
  1152.               break;
  1153.               //
  1154.             case 'F':     // footer id
  1155.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1156.                 HTS_PANIC_PRINTF("Option %F needs to be followed by a blank space, and a footer string");
  1157.                 printf("Example: -%%F \"<!-- Mirrored from %%s by HTTrack Website Copier/"HTTRACK_AFF_VERSION" "HTTRACK_AFF_AUTHORS" -->\"\n");
  1158.                 return -1;
  1159.               } else{
  1160.                 na++;
  1161.                 if (strlen(argv[na])>=254) {
  1162.                   HTS_PANIC_PRINTF("Footer string too long");
  1163.                   return -1;
  1164.                 }
  1165.                 strcpy(httrack.footer,argv[na]);
  1166.               }
  1167.               break;
  1168.             case 'H':                 // debug headers
  1169.               _DEBUG_HEAD=1;
  1170.               break;
  1171.               /*
  1172.             case 'O':
  1173. #if HTS_WIN
  1174.               printf("Warning option -%%O has no effect in this system (chroot)\n");
  1175. #else
  1176.               switch_chroot=1;
  1177. #endif
  1178.               break;
  1179.               */
  1180.             case 'U':                 // setuid
  1181.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1182.                 HTS_PANIC_PRINTF("Option %U needs to be followed by a blank space, and a username");
  1183.                 printf("Example: -%%U smith\n");
  1184.                 return -1;
  1185.               } else {
  1186.                 na++;
  1187. #if HTS_WIN
  1188.                 printf("Warning option -%%U has no effect on this system (setuid)\n");
  1189. #else
  1190. #ifndef HTS_DO_NOT_USE_UID
  1191.                 /* Change the user id and gid */
  1192.                 {
  1193.                   struct passwd* userdef=getpwnam((const char*)argv[na]);
  1194.                   if (userdef) {    /* we'll have to switch the user id */
  1195.                     switch_gid=userdef->pw_gid;
  1196.                     switch_uid=userdef->pw_uid;
  1197.                   }
  1198.                 }
  1199. #else
  1200.                 printf("Warning option -%%U has no effect with this compiled version (setuid)\n");
  1201. #endif
  1202. #endif
  1203.               }
  1204.               break;
  1205.             }
  1206.                     }
  1207.             break;
  1208.             //
  1209.           case '#':  { // non documentΘ (appel de l'interface)
  1210.             com++;
  1211.             switch(*com) {
  1212.             case 'f': httrack.flush=1; break;
  1213.             case 'h':
  1214.               printf("HTTrack version "HTTRACK_VERSION"\n");
  1215.               exit(1);
  1216.               break;
  1217.             case 'p': httrack.aff_progress=1; break;
  1218.             case 'S': httrack.shell=1; break;  // stdin sur un shell
  1219.             case 'K': httrack.keyboard=1; break;  // vΘrifier stdin
  1220.               //
  1221.             case 'L': sscanf(com+1,"%d",&httrack.maxlink); while(isdigit((unsigned char)*(com+1))) com++; break;
  1222.             case 'F': sscanf(com+1,"%d",&httrack.maxfilter); while(isdigit((unsigned char)*(com+1))) com++; break;
  1223.             case 'Z': httrack.makestat=1; break;
  1224.             case 'T': httrack.maketrack=1; break;
  1225.             case 'u': sscanf(com+1,"%d",&httrack.waittime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1226.               
  1227.             case 'R':    // ohh ftp, catch->ftpget
  1228.               HTS_PANIC_PRINTF("Unexpected internal error with -#R command");
  1229.               return -1;        
  1230.               break;
  1231.             case 'P': {     // catchurl
  1232.               help_catchurl(httrack.path_log);
  1233.               return 0;
  1234.                       }
  1235.               break;
  1236.           
  1237.             case '0':   /* test #0 : filters */
  1238.               if (na+2>=argc) {
  1239.                 HTS_PANIC_PRINTF("Option #0 needs to be followed by a filter string and a string");
  1240.                 printf("Example: '-#0' '*.gif' 'foo.gif'\n");
  1241.                 return -1;
  1242.               } else {
  1243.                 if (strjoker(argv[na+2],argv[na+1],NULL,NULL))
  1244.                   printf("%s does match %s\n",argv[na+2],argv[na+1]);
  1245.                 else
  1246.                   printf("%s does NOT match %s\n",argv[na+2],argv[na+1]);
  1247.                 return 0;
  1248.               }
  1249.               break;
  1250.             case '!':
  1251.               if (na+1>=argc) {
  1252.                 HTS_PANIC_PRINTF("Option #! needs to be followed by a commandline");
  1253.                 printf("Example: '-#!' 'echo hello'\n");
  1254.                 return -1;
  1255.               } else {
  1256.                 system(argv[na+1]);
  1257.               }
  1258.               break;
  1259.  
  1260.             default: printf("Internal option %c not recognized\n",*com); break;
  1261.             }
  1262.                      }
  1263.             break; 
  1264.           case 'O':    // output path
  1265.             na++;     // sauter, dΘja traitΘ
  1266.             break;
  1267.           case 'P':    // proxy
  1268.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1269.               HTS_PANIC_PRINTF("Option P needs to be followed by a blank space, and a proxy proxy:port or user:id@proxy:port");
  1270.               printf("Example: -P proxy.myhost.com:8080\n");
  1271.               return -1;
  1272.             } else {
  1273.               char* a;
  1274.               na++;
  1275.               httrack.proxy.active=1;
  1276.               // Rechercher MAIS en partant de la fin α cause de user:pass@proxy:port
  1277.               a = argv[na] + strlen(argv[na]) -1;
  1278.               // a=strstr(argv[na],":");  // port
  1279.               while( ((int) a > (int) argv[na]) && (*a != ':') && (*a != '@') ) a--;
  1280.               if (*a == ':') {  // un port est prΘsent, <proxy>:port
  1281.                 sscanf(a+1,"%d",&httrack.proxy.port);
  1282.                 httrack.proxy.name[0]='\0';
  1283.                 strncat(httrack.proxy.name,argv[na],(int) a-(int) argv[na]);
  1284.               } else {  // <proxy>
  1285.                 httrack.proxy.port=8080;
  1286.                 strcpy(httrack.proxy.name,argv[na]);
  1287.               }
  1288.             }
  1289.             break;
  1290.           case 'F':    // user-agent field
  1291.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1292.               HTS_PANIC_PRINTF("Option F needs to be followed by a blank space, and a user-agent name");
  1293.               printf("Example: -F \"my_user_agent/1.0\"\n");
  1294.               return -1;
  1295.             } else{
  1296.               na++;
  1297.               if (strlen(argv[na])>=62) {
  1298.                 HTS_PANIC_PRINTF("User-agent length too long");
  1299.                 return -1;
  1300.               }
  1301.               strcpy(httrack.user_agent,argv[na]);
  1302.               if (strnotempty(httrack.user_agent))
  1303.                 httrack.user_agent_send=1;
  1304.               else
  1305.                 httrack.user_agent_send=0;    // -F "" dΘsactive l'option
  1306.             }
  1307.             break;
  1308.             //
  1309.           case 'V':    // execute command
  1310.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1311.               HTS_PANIC_PRINTF("Option V needs to be followed by a system-command string");
  1312.               printf("Example: -V \"tar uvf some.tar \\$0\"\n");
  1313.               return -1;
  1314.             } else{
  1315.               na++;
  1316.               if (strlen(argv[na])>=2048) {
  1317.                 HTS_PANIC_PRINTF("System-command length too long");
  1318.                 return -1;
  1319.               }
  1320.               strcpy(httrack.sys_com,argv[na]);
  1321.               if (strnotempty(httrack.sys_com))
  1322.                 httrack.sys_com_exec=1;
  1323.               else
  1324.                 httrack.sys_com_exec=0;    // -V "" dΘsactive l'option
  1325.             }
  1326.             break;
  1327.             //
  1328.           default: {
  1329.             char s[HTS_CDLMAXSIZE];
  1330.             sprintf(s,"invalid option %c\n",*com);
  1331.             HTS_PANIC_PRINTF(s);
  1332.             return -1;
  1333.                    }
  1334.             break;
  1335.           }  // switch
  1336.           com++;    
  1337.         }  // while
  1338.         
  1339.       }  else {  // URL/filters
  1340.         char* a=argv[na];
  1341.         
  1342.         while(is_space(*a)) a++;
  1343.         
  1344.         // ajouter URLs et filters[]
  1345.         while (*a) {
  1346.           char c[2];
  1347.           c[1]='\0';
  1348.           while(is_space(*a)) a++;
  1349.           if (*a) {
  1350.             if (strnotempty(url)) strcat(url," ");  // espace de sΘparation
  1351.             while((!is_space(*a)) && (*a!=0)) { c[0]=*a; strcat(url,c); a++;}
  1352.           }
  1353.         }    
  1354.         
  1355.         
  1356.       }  // if argv=- etc. 
  1357.       
  1358.     }  // for
  1359.   }
  1360.   
  1361. #if BDEBUG==3  
  1362.   printf("URLs/filters=%s\n",url);
  1363. #endif
  1364.  
  1365. #if DEBUG_STEPS
  1366.   printf("Analyzing parameters done\n");
  1367. #endif
  1368.  
  1369.  
  1370. #if HTS_WIN
  1371. #else
  1372. #ifndef HTS_DO_NOT_USE_UID
  1373.   /* Chroot - xxc */
  1374.   if (switch_chroot) {
  1375.     uid_t userid=getuid();
  1376.     //struct passwd* userdef=getpwuid(userid);
  1377.     //if (userdef) {
  1378.     if (!userid) {
  1379.       //if (strcmp(userdef->pw_name,"root")==0) {
  1380.       char rpath[1024];
  1381.       //printf("html=%s log=%s\n",httrack.path_html,httrack.path_log);    // xxc
  1382.       if ((httrack.path_html[0]) && (httrack.path_log[0])) {
  1383.         char *a=httrack.path_html,*b=httrack.path_log,*c=NULL,*d=NULL;
  1384.         c=a; d=b;
  1385.         while ((*a) && (*a == *b)) {
  1386.           if (*a=='/') { c=a; d=b; }
  1387.           a++;
  1388.           b++;
  1389.         }
  1390.  
  1391.         rpath[0]='\0';
  1392.         if (c != httrack.path_html) {
  1393.           if (httrack.path_html[0]!='/')
  1394.             strcat(rpath,"./");
  1395.           strncat(rpath,httrack.path_html,(int) c - (int) httrack.path_html);
  1396.         }
  1397.         {
  1398.           char tmp[1024];
  1399.           strcpy(tmp,c); strcpy(httrack.path_html,tmp);
  1400.           strcpy(tmp,d); strcpy(httrack.path_log,tmp);
  1401.         }
  1402.       } else {
  1403.         strcpy(rpath,"./");
  1404.         strcpy(httrack.path_html,"/");
  1405.         strcpy(httrack.path_log,"/");
  1406.       }
  1407.       if (rpath[0]) {
  1408.         printf("[changing root path to %s (path_data=%s,path_log=%s)]\n",rpath,httrack.path_html,httrack.path_log);
  1409.         if (chroot(rpath)) {
  1410.           printf("ERROR! Can not chroot to %s!\n",rpath);
  1411.           exit(0);
  1412.         }
  1413.         if (chdir("/")) {     /* new root */
  1414.           printf("ERROR! Can not chdir to %s!\n",rpath);
  1415.           exit(0);
  1416.         }
  1417.       } else
  1418.         printf("WARNING: chroot not possible with these paths\n");
  1419.     }
  1420.     //}
  1421.   }
  1422.  
  1423.   /* Setuid */
  1424.   if (switch_uid>=0) {
  1425.     printf("[setting user/group to %d/%d]\n",switch_uid,switch_gid);
  1426.     if (setgid(switch_gid))
  1427.       printf("WARNING! Can not setgid to %d!\n",switch_gid);
  1428.     if (setuid(switch_uid))
  1429.       printf("WARNING! Can not setuid to %d!\n",switch_uid);
  1430.   }
  1431.  
  1432.   /* Final check */
  1433.   {
  1434.     uid_t userid=getuid();
  1435.     if (!userid) {              /* running as r00t */
  1436.       printf("WARNING! You are running this program as root!\n");
  1437.       printf("It might be a good idea to use the -%%U option to change the userid:\n");
  1438.       printf("Example: -%%U smith\n\n");
  1439.     }
  1440.   }
  1441. #endif
  1442. #endif
  1443.   
  1444.   //printf("WARNING! This is *only* a beta-release of HTTrack\n");
  1445.   io_flush;
  1446.   
  1447. #if DEBUG_STEPS
  1448.   printf("Cache & log settings\n");
  1449. #endif
  1450.   
  1451.   // on utilise le cache..
  1452.   // en cas de prΘsence des deux versions, garder la version la plus avancΘe,
  1453.   // cad la version contenant le plus de fichiers  
  1454.   if (httrack.cache) {
  1455.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {   // problemes..
  1456.       if (fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx"))) { 
  1457.         if (fexist(fconcat(httrack.path_log,"hts-cache/old.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) {
  1458.           // switcher si new<32Ko et old>65Ko (tailles arbitraires) ?
  1459.           // ce cas est peut Ωtre une erreur ou un crash d'un miroir ancien, prendre
  1460.           // alors l'ancien cache
  1461.           if (fsize(fconcat(httrack.path_log,"hts-cache/new.dat"))<32768) {
  1462.             if (fsize(fconcat(httrack.path_log,"hts-cache/old.dat"))>65536) {
  1463.               if (fsize(fconcat(httrack.path_log,"hts-cache/old.dat")) > fsize(fconcat(httrack.path_log,"hts-cache/new.dat"))) {
  1464.                 remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  1465.                 remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  1466.                 rename(fconcat(httrack.path_log,"hts-cache/old.dat"),fconcat(httrack.path_log,"hts-cache/new.dat"));
  1467.                 rename(fconcat(httrack.path_log,"hts-cache/old.ndx"),fconcat(httrack.path_log,"hts-cache/new.ndx"));  
  1468.                 //} else {  // ne rien faire
  1469.                 //  remove("hts-cache/old.dat");
  1470.                 //  remove("hts-cache/old.ndx");
  1471.               }
  1472.             }
  1473.           }
  1474.         }
  1475.       }
  1476.     }
  1477.   }
  1478.  
  1479.   // DΘbuggage des en tΩtes
  1480.   if (_DEBUG_HEAD) {
  1481.     ioinfo=fopen(fconcat(httrack.path_log,"hts-ioinfo.txt"),"wb");
  1482.   }
  1483.   
  1484.   {
  1485.     char n_lock[256];
  1486.     // on peut pas avoir un affichage ET un fichier log
  1487.     // ca sera pour la version 2
  1488.     if (httrack_logmode==1) {
  1489.       httrack.log=stdout;
  1490.       httrack.errlog=stderr;
  1491.     } else if (httrack_logmode>=2) {
  1492.       // deux fichiers log
  1493.       structcheck(httrack.path_log);
  1494.       if (fexist(fconcat(httrack.path_log,"hts-log.txt")))
  1495.         remove(fconcat(httrack.path_log,"hts-log.txt"));
  1496.       if (fexist(fconcat(httrack.path_log,"hts-err.txt")))
  1497.         remove(fconcat(httrack.path_log,"hts-err.txt"));
  1498.  
  1499.       httrack.log=fopen(fconcat(httrack.path_log,"hts-log.txt"),"w");
  1500.       if (httrack_logmode==2)
  1501.         httrack.errlog=fopen(fconcat(httrack.path_log,"hts-err.txt"),"w");
  1502.       else
  1503.         httrack.errlog=httrack.log;
  1504.       if (httrack.log==NULL) {
  1505.         char s[HTS_CDLMAXSIZE];
  1506.         sprintf(s,"Unable to create log file %s",fconcat(httrack.path_log,"hts-log.txt"));
  1507.         HTS_PANIC_PRINTF(s);
  1508.         return -1;
  1509.       } else if (httrack.errlog==NULL) {
  1510.         char s[HTS_CDLMAXSIZE];
  1511.         sprintf(s,"Unable to create log file %s",fconcat(httrack.path_log,"hts-err.txt"));
  1512.         HTS_PANIC_PRINTF(s);
  1513.         return -1;
  1514.       }
  1515.  
  1516.     } else {
  1517.       httrack.log=NULL;
  1518.       httrack.errlog=NULL;
  1519.     }
  1520.     
  1521.     // un petit lock-file pour indiquer un miroir en cours, ainsi qu'un Θventuel fichier log
  1522.     {
  1523.       FILE* fp=NULL;
  1524.       //int n=0;
  1525.       char t[256];
  1526.       time_local_rfc822(t);    // faut bien que ca serve quelque part l'heure RFC1945 arf'
  1527.       
  1528.       /* readme for information purpose */
  1529.       {
  1530.         FILE* fp=fopen(fconcat(httrack.path_log,"hts-cache/readme.txt"),"wb");
  1531.         if (fp) {
  1532.           fprintf(fp,"What's in this folder?"LF);
  1533.           fprintf(fp,""LF);
  1534.           fprintf(fp,"This folder (hts-cache) has been generated by WinHTTrack "HTTRACK_VERSION""LF);
  1535.           fprintf(fp,"and is used for updating this website."LF);
  1536.           fprintf(fp,"(The HTML website structure is stored here to allow fast updates)"LF""LF);
  1537.           fprintf(fp,"DO NOT delete this folder unless you do not want to update the mirror in the future!!"LF);
  1538.           fprintf(fp,"(you can safely delete old.dat, old.ndx and old.lst files, however)"LF);
  1539.           fclose(fp);
  1540.         }
  1541.       }
  1542.  
  1543.       sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"));
  1544.       //sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"),n);
  1545.       /*do {
  1546.         if (!n)
  1547.           sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"),n);
  1548.         else
  1549.           sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress%d.lock"),n);
  1550.         n++;
  1551.       } while((fexist(n_lock)) && httrack.quiet);      
  1552.       if (fexist(n_lock)) {
  1553.         if (!recuperer) {
  1554.           remove(n_lock);
  1555.         }
  1556.       }*/
  1557.  
  1558.       // vΘrifier existence de la structure
  1559.       structcheck(httrack.path_html);
  1560.       structcheck(httrack.path_log);
  1561.      
  1562.       // reprise/update
  1563.       if (httrack.cache) {
  1564.         FILE* fp;
  1565.         int i;
  1566. #if HTS_WIN
  1567.         mkdir(fconcat(httrack.path_log,"hts-cache"));
  1568. #else
  1569.         mkdir(fconcat(httrack.path_log,"hts-cache"),HTS_PROTECT_FOLDER);
  1570. #endif
  1571.         fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"wb");
  1572.         if (fp) {
  1573.           for(i=0+1;i<argc;i++) {
  1574.             if ( ((strchr(argv[i],' ')!=NULL) || (strchr(argv[i],'"')!=NULL) || (strchr(argv[i],'\\')!=NULL)) && (argv[i][0]!='"')  ) {
  1575.               int j;
  1576.               fprintf(fp,"\"");
  1577.               for(j=0;j<(int) strlen(argv[i]);j++) {
  1578.                 if (argv[i][j]==34)
  1579.                   fprintf(fp,"\\\"");
  1580.                 else if (argv[i][j]=='\\')
  1581.                   fprintf(fp,"\\\\");
  1582.                 else
  1583.                   fprintf(fp,"%c",argv[i][j]);
  1584.               }
  1585.               fprintf(fp,"\"");
  1586.             } else if (strnotempty(argv[i])==0) {   // ""
  1587.               fprintf(fp,"\"\"");
  1588.             } else {   // non critique
  1589.               fprintf(fp,"%s",argv[i]);
  1590.             }
  1591.             if (i<argc-1)
  1592.               fprintf(fp," ");
  1593.           }
  1594.           fprintf(fp,LF);
  1595.           fprintf(fp,"File generated automatically on %s, do NOT edit"LF,t);
  1596.           fprintf(fp,LF);
  1597.           fprintf(fp,"To update a mirror, just launch httrack without any parameters"LF);
  1598.           fprintf(fp,"The existing cache will be used (and modified)"LF);
  1599.           fprintf(fp,"To have other options, retype all parameters and launch HTTrack"LF);
  1600.           fprintf(fp,"To continue an interrupted mirror, just launch httrack without any parameters"LF);
  1601.           fprintf(fp,LF);
  1602.           fclose(fp); fp=NULL;
  1603.         //} else if (httrack.debug>1) {
  1604.         //  printf("! FileOpen error, \"%s\"\n",strerror(errno));
  1605.         }
  1606.       }
  1607.       
  1608.       // petit message dans le lock
  1609.       if ( (fp=fopen(n_lock,"wb"))!=NULL) {
  1610.         int i;
  1611.         fprintf(fp,"Mirror in progress since %s .. please wait!"LF,t);
  1612.         for(i=0;i<argc;i++) {
  1613.           if (strchr(argv[i],' ')==NULL)
  1614.             fprintf(fp,"%s ",argv[i]);
  1615.           else    // entre ""
  1616.             fprintf(fp,"\"%s\" ",argv[i]);
  1617.         }
  1618.         fprintf(fp,LF);
  1619.         fclose(fp); fp=NULL;
  1620.       }
  1621.       
  1622.       // fichier log        
  1623.       if (httrack.log)     {
  1624.         int i;
  1625.         fprintf(httrack.log,"HTTrack"HTTRACK_VERSION" launched on %s at %s"LF,t,url);
  1626.         fprintf(httrack.log,"(");
  1627.         for(i=0;i<argc;i++) {
  1628.           if ((strchr(argv[i],' ')==NULL) || (strchr(argv[i],'\"')))
  1629.             fprintf(httrack.log,"%s ",argv[i]);
  1630.           else    // entre "" (si espace(s) et pas dΘja de ")
  1631.             fprintf(httrack.log,"\"%s\" ",argv[i]);
  1632.         }
  1633.         fprintf(httrack.log,")"LF);
  1634.         fprintf(httrack.log,LF);
  1635.         fprintf(httrack.log,"Information, Warnings and Errors reported for this mirror:"LF);
  1636.       }
  1637.  
  1638.       if (httrack_logmode)     {
  1639.         printf("Mirror launched on %s by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS""LF,t);
  1640.         if (httrack.wizard==0) {
  1641.           printf("mirroring %s with %d levels, %d sockets,t=%d,s=%d,logm=%d,lnk=%d,mdg=%d\n",url,httrack.depth,httrack.maxsoc,httrack.travel,httrack.seeker,httrack_logmode,httrack.urlmode,httrack.getmode);
  1642.         } else {    // the magic wizard
  1643.           printf("mirroring %s with the wizard help..\n",url);
  1644.         }
  1645.       }
  1646.     }
  1647.     
  1648.     io_flush;
  1649.  
  1650. #if HTS_ANALYSTE
  1651.   hts_htmlcheck_init();
  1652. #endif
  1653.  
  1654.   // dΘtourner SIGHUP etc.
  1655. #if HTS_WIN
  1656.   signal( SIGINT  , sig_ask    );   // ^C
  1657.   signal( SIGTERM , sig_finish );   // kill <process>
  1658. #else
  1659.   signal( SIGHUP  , sig_back   );   // close window
  1660.   signal( SIGTSTP , sig_back   );   // ^Z
  1661.   signal( SIGTERM , sig_finish );   // kill <process>
  1662.   signal( SIGINT  , sig_ask    );   // ^C
  1663. /*
  1664. deprecated - see SIGCHLD
  1665. #ifndef HTS_DO_NOT_SIGCLD
  1666.   signal( SIGCLD  , sig_ignore );   // child change status
  1667. #endif
  1668. */
  1669.   signal( SIGCHLD , sig_ignore );   // child change status
  1670. #endif
  1671. #if DEBUG_STEPS
  1672.   printf("Launching the mirror\n");
  1673. #endif
  1674.   
  1675.  
  1676.     // Lancement du miroir
  1677.     // ------------------------------------------------------------
  1678.     if (httpmirror(url,httrack)==0) {
  1679.       printf("Error during operation (see log file), site has not been successfully mirrored\n");
  1680.     } else {
  1681.       if  (httrack.shell) {
  1682.         HTT_REQUEST_START;
  1683.         HT_PRINT("TRANSFER DONE"LF);
  1684.         HTT_REQUEST_END
  1685.       } else {
  1686.         printf("Done.\n");
  1687.       }
  1688.     }
  1689.     // ------------------------------------------------------------
  1690.  
  1691.     if (exit_xh ==1) {
  1692.       if (httrack.log) {
  1693.         fprintf(httrack.log,"* * MIRROR ABORTED! * *\nThe current temporary cache is required for any update operation and only contains data downloaded during the present aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files]\n");
  1694.       }
  1695.     }
  1696.  
  1697. #if HTS_ANALYSTE
  1698.   hts_htmlcheck_uninit();
  1699. #endif
  1700.     
  1701.     if (httrack_logmode==1) {
  1702.       if (httrack.errlog == httrack.log) httrack.errlog=NULL;
  1703.       if (httrack.log) { fclose(httrack.log); httrack.log=NULL; }
  1704.       if (httrack.errlog) { fclose(httrack.errlog); httrack.errlog=NULL; }
  1705.     }  
  1706.     
  1707.     // DΘbuggage des en tΩtes
  1708.     if (_DEBUG_HEAD) {
  1709.       if (ioinfo) {
  1710.         fclose(ioinfo);
  1711.       }
  1712.     }
  1713.     
  1714.     // supprimer lock
  1715.     remove(n_lock);
  1716.   }
  1717.   
  1718.   if (x_argvblk)
  1719.     freet(x_argvblk);
  1720.  
  1721. #if HTS_WIN
  1722. #if HTS_ANALYSTE!=2
  1723. //  WSACleanup();    // ** non en cas de thread tjs prΘsent!..
  1724. #endif
  1725. #endif
  1726. #if HTS_TRACE_MALLOC
  1727.   hts_freeall();
  1728. #endif
  1729.  
  1730.   printf("Thanks for using HTTrack!\n");
  1731.   io_flush;
  1732.   return 0;    // OK
  1733. }
  1734.  
  1735.  
  1736. // main() subroutines
  1737.  
  1738. // vΘrifier chemin path
  1739. void check_path(char* s) {
  1740.   int i;
  1741.   expand_home(s);         /* expand from ~/ to /home/smith/ */
  1742.   for(i=0;i<(int) strlen(s);i++)    // conversion \ -> /
  1743.     if (s[i]=='\\')
  1744.       s[i]='/';
  1745.   if (s[strlen(s)-1]!='/')    // ajouter slash α la fin
  1746.     strcat(s,"/");
  1747. }
  1748.  
  1749. // dΘtermine si l'argument est une option
  1750. int cmdl_opt(char* s) {
  1751.   if (s[0]=='-') {  // c'est peut Ωtre une option
  1752.     if (strchr(s,'.')!=NULL)
  1753.       return 0;    // sans doute un -www.truc.fr (note: -www n'est pas compris)
  1754.     else if (strchr(s,'/')!=NULL)
  1755.       return 0;    // idem, -*cgi-bin/
  1756.     else if (strchr(s,'*')!=NULL)
  1757.       return 0;    // joker, idem
  1758.     else
  1759.       return 1;
  1760.   } else return 0;
  1761. }
  1762.  
  1763.  
  1764.  
  1765.